﻿using System;
using DBProvider.Providers;
using DBProvider;

namespace HealthCommunity
{
	public partial class Register2 : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				txtProtocal.Text = System.Configuration.ConfigurationManager.AppSettings["UserProtocal"];
				var qqid = CommonHelper.QueryString("qqid");

				this.txtQQID.Text = qqid;
				this.txtEMail.Text = qqid;

				BindDrugStore();

				BindStateProvince();
			}
		}

		private void BindDrugStore()
		{
			var drugStoreProvider = new DrugStoreProvider();

			ddlDrugStore.Items.Clear();
			ddlDrugStore.DataSource = drugStoreProvider.GetAllDrugStore();
			ddlDrugStore.DataBind();
		}

		protected void btnSubmit_Click(object sender, EventArgs e)
		{
			var theUser = new User();
			theUser.Address = txtAddress.Text.Trim();
			theUser.Email = txtEMail.Text.Trim() + "@qq.com";
			theUser.IsAdmin = 0;
			theUser.QQ = txtQQID.Text.Trim();
			theUser.SecretAnswer1 = txtSecretAnswer1.Text.Trim();
			theUser.SecretAnswer2 = txtSecretAnswer2.Text.Trim();
			theUser.SecretQuestion1 = txtSecretQuestion1.Text.Trim();
			theUser.SecretQuestion2 = txtSecretQuestion2.Text.Trim();
			theUser.Credits = 200;
			if(rblSexFemale.Checked)
			{
				theUser.Sex = 2;
			}
			else if(rblSexMale.Checked)
			{
				theUser.Sex = 1;
			}
			else if(rblSexKeepSecret.Checked)
			{
				theUser.Sex = 0;
			}
			else
			{
				theUser.Sex = -1;
			}
			theUser.ShengFenZheng = txtSFZ.Text.Trim();
			theUser.UserCName = txtChName.Text.Trim();
			theUser.UserName = txtQQID.Text.Trim();
			theUser.UserPassword = txtPWD.Text;
			theUser.UserState = (int)UserState.Active;

			var selectedDrugStore = 0;
			if (int.TryParse(ddlDrugStore.SelectedValue, out selectedDrugStore))
			{
				theUser.DrugStoreID = selectedDrugStore;
			}

			try
			{
				var userProvider = new UserProvider();

				var role = userProvider.GetRole("storemember");

				if(role != null)
				{
					theUser.RoleID = role.RoleID;
				}

				var msg = string.Empty;
				var theNewUser = userProvider.CreateUser(theUser, out msg);
				if(theNewUser != null)
				{
					Server.Transfer("RegisterComplete.aspx?userid=" + theNewUser.UserID.ToString());
				}
				else
				{
					ltrMsg.Text = "用户添加失败了";
				}
			}
			catch(Exception exc)
			{
				ltrMsg.Text = exc.Message;
			}
		}

		protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
		{
			var selectedItem = ddlProvince.SelectedValue;

			if (!string.IsNullOrEmpty(selectedItem))
			{
				var selectedProvinceID = int.Parse(selectedItem);

				var areaProvider = new AreaProvider();
				var cities = areaProvider.GetCityByStateProvince(selectedProvinceID);

				ddlCity.DataSource = cities;
				ddlCity.DataBind();
			}
		}

		private void BindStateProvince()
		{
			var areaProvider = new AreaProvider();

			ddlProvince.Items.Clear();
			ddlProvince.DataSource = areaProvider.GetAllStateProvince();
			ddlProvince.DataBind();
		}
	}
}